//
//  STCollectionViewController.swift
//  iPhone
//
//  Created by Toj on 7/11/23.
//

import UIKit

// MARK: - 懒加载(属性), 变量, 初始化init
class STCollectionViewController: STBaseViewController {
    
    var topInset: CGFloat = .nav_barFullHeight
    
    private var colors = kColors
    
    private lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical
        // cell之间 垂直间距
        layout.minimumLineSpacing = 8
        // cell之间 水平间距
        layout.minimumInteritemSpacing = 0
        layout.itemSize = CGSize(width: .dv_width, height: 44)
        
        let view = UICollectionView(frame: .zero, collectionViewLayout: layout)
        view.scrollsToTop = true
        view.showsVerticalScrollIndicator = false
        view.showsHorizontalScrollIndicator = false
        view.backgroundColor = .white
        view.register(cellClass: STCollectionViewCell.self)
        view.dataSource = self
        view.delegate = self
        view.contentInset = UIEdgeInsets(top: topInset, left: 0, bottom: 0, right: 0)
        return view
    }()
}

// MARK: - 生命周期
extension STCollectionViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupMakeInitialize()
        setupMakeAddSubviews()
        setupMakeLayoutSubviews()
    }
}

// MARK: - 点击事件
@objc private extension STCollectionViewController {
    
}

// MARK: - Public
extension STCollectionViewController {
    
}

// MARK: - Private
private extension STCollectionViewController {
    
}

// MARK: - 代理事件
extension STCollectionViewController: UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return colors.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifierClass: STCollectionViewCell.self, for: indexPath)
        let pointer = Unmanaged<AnyObject>.passUnretained(cell as AnyObject).toOpaque()
        
        cell.numberString = "collectionView address: \(pointer), num:" + String(indexPath.row)
        let color = colors[indexPath.row]
        cell.contentView.backgroundColor = color.to_color
        return cell
    }
}

extension STCollectionViewController: UICollectionViewDelegate {

    func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        guard let _ = cell as? STCollectionViewCell else { return }
        
    }
    
    func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        guard let _ = cell as? STCollectionViewCell else { return }
        
    }
}

// MARK: - 初始化设置
private extension STCollectionViewController {
    
    func setupMakeAddSubviews() {
        view.addSubview(collectionView)
    }
    
    func setupMakeLayoutSubviews() {
        collectionView.snp.makeConstraints { make in
            make.edges.equalTo(view)
        }
    }
    
    func setupMakeInitialize() {
        
    }
}
